import { createSlice } from "@reduxjs/toolkit"
export const slice = createSlice({
    name: "date",
    initialState: {
        todoList: {},
    },
    reducers: {
        addTodoItem: (state, action) => {
            const { date, item } = action.payload
            item.id = new Date().getTime()
            let dayList = state.todoList[date.format("YYYY-MM-DD")]
            if (dayList) {
                dayList.unshift(item)
            } else {
                dayList = [item]
            }
            state.todoList[date.format("YYYY-MM-DD")] = dayList
        },
        dltTodoItem: (state, action) => {
            const { date } = action.payload
            delete state.todoList[date]
        },
        updTodoItem: (state, action) => {
            const { date, item, index } = action.payload
            state.todoList[date.format("YYYY-MM-DD")][index] = item
        },
    },
})

export const { addTodoItem, dltTodoItem, updTodoItem } = slice.actions
export default slice.reducer
